<template>
    <div class="addProd" ref="doms" id="doms">
        <div class="box flex-b">
            <div class="left">
                <div class="prod-title flex-a" id="basic">
                    <!-- <div class="basic">基本信息<span>引用上一次填写</span></div> -->
                    <div class="basic">基本信息</div>
                    <el-button type="warning" style="padding:12px 50px;margin-left: 10px;" class="btn-plain"
                        @click="back">返回上一页</el-button>
                </div>
                <div class="prod-content">
                    <el-form ref="form" :model="form">
                        <!-- <div>
                            <p>条形码</p>
                            <el-input v-model="form.name" class="input" placeholder="请输入条形码" clearable></el-input>
                            <el-button type="warning" class="btn">生成</el-button>
                        </div> -->
                        <!-- <div>
                            <p>更多条形码<span>(多个请用“,”隔开)</span></p>
                            <el-input type="textarea" :rows="4" placeholder="例如：0001,0002" v-model="form.desc"
                                clearable></el-input>
                        </div> -->
                        <div class="flex-a">
                            <div class="mr">
                                <p>商品名称</p>
                                <el-input v-model="form.name" @input="inputName" class="input" placeholder="请输入商品名称"
                                    clearable></el-input>
                            </div>
                            <div>
                                <p>拼音码</p>
                                <el-input v-model="form.pinyin" class="input" placeholder="请输入拼音码" clearable></el-input>
                            </div>
                        </div>
                        <div>
                            <p>商品分类</p>
                            <el-select v-model="form.topCate" @change="changeClass" filterable placeholder="请选择分类"
                                class="input" clearable>
                                <el-option :label="item.name" :value="item.id+''" v-for="item in classList"
                                    :key="item.id"></el-option>
                            </el-select>
                            <el-select v-model="form.sedCate" v-if="form.topCate" filterable placeholder="请选择子分类"
                                class="input" style="margin-left:10px" clearable>
                                <el-option :label="item.name" :value="item.id+''" v-for="item in subClassList"
                                    :key="item.id"></el-option>
                            </el-select>
                            <!-- <div class="tip">(没找到分类？请 <span @click="addSort">新增分类</span>)</div> -->
                        </div>
                        <div>
                            <p>规格型号</p>
                            <el-input v-model="form.spetModel" class="input" placeholder="请输入规格型号" clearable></el-input>
                        </div>
                        <div>
                            <p>供应商</p>
                            <el-select v-model="form.datProdInfocol" filterable placeholder="请选择供应商" class="input"
                                clearable>
                                <el-option :label="item.supName" :value="item.id+''" v-for="item in suppLiserList"
                                    :key="item.id"></el-option>
                            </el-select>
                            <!-- <div class="tip">(没找到供应商？请 <span @click="addSupplier">新增供应商</span>)</div> -->
                        </div>
                        <div>
                            <p>品牌</p>
                            <el-select v-model="form.brand" filterable placeholder="请选择品牌" class="input" clearable>
                                <el-option :label="item.name" :value="item.id+''" v-for="item in brandList"
                                    :key="item.id"></el-option>
                            </el-select>
                            <!-- <div class="tip">(没找到品牌？请 <span @click="addBrand">新增品牌</span>)</div> -->
                        </div>
                        <div>
                            <p>保质期</p>
                            <el-input v-model="form.sellByDate" class="input" placeholder="请输入保质期" clearable></el-input>
                            <el-select v-model="form.dateUnit" placeholder="" class="input select">
                                <el-option label="天" value="d"></el-option>
                                <el-option label="月" value="m"></el-option>
                                <el-option label="年" value="y"></el-option>
                            </el-select>
                        </div>
                        <div>
                            <p>适用宠物类型</p>
                            <el-select v-model="form.suitPetType" placeholder="请选择适用宠物类型" class="input" clearable>
                                <el-option :label="item.label" :value="item.value+''" v-for="item in suitPetTypes"
                                    :key="item.value"></el-option>
                            </el-select>
                        </div>
                        <div>
                            <p>单位名称</p>
                            <el-input v-model="form.unit" class="input" placeholder="请输入单位名称" clearable></el-input>
                        </div>
                        <div>
                            <p>零售价</p>
                            <el-input v-model="form.retPrice" class="input input-radius" placeholder="请输入零售价" clearable>
                                <template slot="prepend">￥</template>
                            </el-input>
                        </div>
                        <div>
                            <p>会员价</p>
                            <el-input v-model="form.memPrice" class="input input-radius" placeholder="请输入会员价" clearable>
                                <template slot="prepend">￥</template>
                            </el-input>
                        </div>
                    </el-form>
                </div>
                <div class="prod-title flex-a" id="setting">
                    <div class="basic">高级设置</div>
                </div>
                <div class="prod-content">
                    <!-- <div class="flex">
                        <div class="mr-60">
                            <el-checkbox v-model="form.ck1">新建初始库存</el-checkbox>
                        </div>
                        <div class="mr-60" v-if="form.ck1">
                            <p>初始库存</p>
                            <el-input v-model="form.name" class="input input-radius-right" placeholder="初始库存" clearable>
                                <template slot="append">个</template>
                            </el-input>
                        </div>
                        <div v-if="form.ck1">
                            <p>
                                标准进货价
                                <el-tooltip class="item" effect="dark" content="拆散的商品不计算标准进货价，拆散后的商品标准进货价默认为0。"
                                    placement="top">
                                    <i class="el-icon-question"></i>
                                </el-tooltip>
                            </p>
                            <el-input v-model="form.name" class="input input-radius" placeholder="初始库存" clearable>
                                <template slot="prepend">￥</template>
                            </el-input>
                        </div>
                    </div> -->
                    <div class="mb-50">
                        <el-checkbox v-model="form.isNoModel">支持负库存销售</el-checkbox>
                    </div>
                    <div class="flex">
                        <div class="mr-60">
                            <el-checkbox v-model="form.alertOpen">开启库存预警</el-checkbox>
                            <el-tooltip class="item" effect="dark" content="库存预警指的是当商品库存数达到所设置的临界点时，系统就会提示库存不足。"
                                placement="top">
                                <i class="el-icon-question"></i>
                            </el-tooltip>
                        </div>
                        <div v-if="form.alertOpen">
                            库存少于
                            <el-input v-model="form.alert" class="input" placeholder="" clearable></el-input>
                            个提醒
                        </div>
                    </div>
                    <div class="mb-50">
                        <el-checkbox v-model="form.isDisMem">参与会员打折</el-checkbox>
                        <el-tooltip class="item" effect="dark" content="参与会员折扣指的是该商品是否参与会员的会员卡折扣。不参与即使用会员卡时不会进行折扣或会员价。"
                            placement="top">
                            <i class="el-icon-question"></i>
                        </el-tooltip>
                    </div>
                    <div class="mb-50">
                        <el-checkbox v-model="form.isPrchRem">复购提醒</el-checkbox>
                        <el-tooltip class="item" effect="dark"
                            content="勾选了即是需要提醒复购的商品，结账后可设置复购商品的提醒时间，也会提前两天告知店家处理提醒，系统也可设置自动提醒（前提是打开自动提醒开关）"
                            placement="top">
                            <i class="el-icon-question"></i>
                        </el-tooltip>
                    </div>
                </div>
                <div class="prod-title flex-a" id="more">
                    <div class="basic">更多信息</div>
                </div>
                <div class="prod-content">
                    <el-form ref="form" :model="form">
                        <div>
                            <p>
                                配送重量
                                <el-tooltip class="item" effect="dark"
                                    content="配送重量用于用户在小程序下单选择快递配送或送货上门时参与运费模板公式计算，从而得出运费。" placement="top">
                                    <i class="el-icon-question"></i>
                                </el-tooltip>
                            </p>
                            <el-input v-model="form.weight" class="input input-radius-right" placeholder="请输入配送重量" clearable>
                                <template slot="append">kg</template>
                            </el-input>
                        </div>
                        <div class="suggest">商品图片 <span>建议上传图片尺寸为 1:1 (600px*600px) / 4:3 (600px*450px)</span></div>
                        <div>
                            <el-upload class="upload-demo" drag :action="action" :show-file-list="false"
                                :headers="headers" :on-success="handleAvatarSuccess">
                                <img v-if="form.url" :src="form.url" class="avatar" />
                                <div v-else>
                                    <i class="el-icon-upload"></i>
                                    <div class="el-upload__text">新增</div>
                                </div>
                            </el-upload>
                        </div>
                        <div>商品详情说明</div>
                    </el-form>
                </div>
                <div class="mb-50">
                    <quill-editor v-model="form.text" ref="myQuillEditor" :options="editorOption"
                        @blur="onEditorBlur($event)" @focus="onEditorFocus($event)" @change="onEditorChange($event)"
                        @ready="onEditorReady($event)">
                    </quill-editor>
                </div>
                <div>
                    <el-button type="primary" style="padding:12px 71px" class="btn" @click="onSubmit">提交</el-button>
                    <el-button type="warning" style="padding:12px 50px" class="btn-plain"
                        @click="back">返回上一页</el-button>
                </div>
            </div>
            <div class="right">
                <ul>
                    <li :class="tab == 'basic' ? 'active' : ''" @click="jumpTo('basic')">基本信息</li>
                    <li :class="tab == 'setting' ? 'active' : ''" @click="jumpTo('setting')">高级设置</li>
                    <li :class="tab == 'more' ? 'active' : ''" @click="jumpTo('more')">更多信息</li>
                </ul>
            </div>
        </div>
        <!-- 新增分类--新增供应商--品牌弹窗 -->
        <el-dialog :title="srotDialogTitle" :visible.sync="sortDialogVisible" width="25%" class="center">
            <el-form :model="sortForm" :rules="sortRules" label-width="100px" class="input">
                <el-form-item label="分类名称" prop="name" v-if="srotDialogTitle == '新增分类' || srotDialogTitle == '新增子分类'">
                    <el-input v-model="sortForm.name" autocomplete="off" placeholder="请输入分类名称（建议 5 字以内）"></el-input>
                </el-form-item>
                <el-form-item label="分类标签" prop="name" v-if="srotDialogTitle == '新增子分类'">
                    <el-select v-model="form.topCate" filterable placeholder="请选择分类标签" class="input" style="width:100%"
                        clearable>
                        <el-option-group v-for="group in classList" :key="group.id" :label="group.name">
                            <el-option v-for="item in group.list" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-option-group>
                    </el-select>
                </el-form-item>
                <el-form-item label="品牌名称" prop="name" v-if="srotDialogTitle == '新增品牌'">
                    <el-input v-model="sortForm.name" autocomplete="off" placeholder="请输入品牌名称（建议 5 字以内）"></el-input>
                </el-form-item>
                <div v-if="srotDialogTitle == '新增供应商'">
                    <el-form-item label="供应商名称" prop="supName">
                        <el-input v-model="sortForm.supName" autocomplete="off"
                            placeholder="请输入供应商名称（建议 5 字以内）"></el-input>
                    </el-form-item>
                    <el-form-item label="公司电话" prop="supCoName">
                        <el-input v-model="sortForm.supCoName" autocomplete="off" placeholder="请输入公司电话"></el-input>
                    </el-form-item>
                    <el-form-item label="联系人" prop="supContName">
                        <el-input v-model="sortForm.supContName" autocomplete="off" placeholder="请输入联系人"></el-input>
                    </el-form-item>
                    <el-form-item label="联系电话" prop="supContPhone">
                        <el-input v-model="sortForm.supContPhone" autocomplete="off" placeholder="请输入联系电话"></el-input>
                    </el-form-item>
                    <el-form-item label="开户行" prop="supBank">
                        <el-input v-model="sortForm.supBank" autocomplete="off" placeholder="请输入开户行"></el-input>
                    </el-form-item>
                    <el-form-item label="户名" prop="supBankNo">
                        <el-input v-model="sortForm.supBankNo" autocomplete="off" placeholder="请输入户名"></el-input>
                    </el-form-item>
                    <el-form-item label="银行账户" prop="supBankName">
                        <el-input v-model="sortForm.supBankName" autocomplete="off" placeholder="请输入银行账户"></el-input>
                    </el-form-item>
                </div>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button class="btn" style="padding:12px 70px" @click="submitForm">提 交</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import pinyin from 'js-pinyin'
import { supplierSelectList } from "@/api/pet/supplier"
import { brandSelectList } from "@/api/pet/brand"
import { getClassList } from "@/api/pet/class"
import { addProdInfo, getProdInfo, updateProdInfo } from "@/api/pet/prodInfo";

import { quillEditor } from 'vue-quill-editor'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { getToken } from "@/utils/auth";
import { getSupplier } from "../../../api/pet/supplier"

export default {
    name: "addProd",
    components: {
        quillEditor
    },
    data() {
        return {
            id: '',
            suppLiserList: [],//供应商下拉列表
            brandList: [],//品牌下拉列表
            classList: [],//分类--子分类二位数组
            subClassList: [],//子分类
            form: {},
            action: process.env.VUE_APP_BASE_API + '/pet/upload/comFile',
            headers: {
                Authorization: "Bearer " + getToken(),
            },
            suitPetTypes: [
                {
                    label: '猫用',
                    value: 1
                },
                {
                    label: '狗用',
                    value: 2
                },
                {
                    label: '猫狗通用',
                    value: 3
                },
                {
                    label: '小宠',
                    value: 4
                },
                {
                    label: '其他',
                    value: 5
                }
            ],//宠物类型
            // 富文本编辑器配置
            editorOption: {
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'], // 加粗 斜体 下划线 删除线
                        ['blockquote', 'code-block'], // 引用  代码块
                        [{ header: 1 }, { header: 2 }], // 1、2 级标题
                        [{ list: 'ordered' }, { list: 'bullet' }], // 有序、无序列表
                        [{ script: 'sub' }, { script: 'super' }], // 上标/下标
                        [{ indent: '-1' }, { indent: '+1' }], // 缩进
                        [{ direction: 'rtl' }], // 文本方向
                        [{ size: ['12', '14', '16', '18', '20', '22', '24', '28', '32', '36'] }], // 字体大小
                        [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
                        [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
                        // [{ font: ['songti'] }], // 字体种类
                        [{ align: [] }], // 对齐方式
                        ['clean'], // 清除文本格式
                        ['image', 'video'] // 链接、图片、视频
                    ]
                },
                placeholder: '请输入正文'
            },
            content: '',
            target: 0,
            tab: 'basic',
            sortDialogVisible: false,//新增分类弹窗
            srotDialogTitle: '',
            sortForm: {},
            sortRules: {
                name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' }
                ],
            },
        };
    },
    created() {
        this.getSupplierList()//获取供应商下拉列表
        this.getBrandList()//获取品牌下拉列表
        this.getClassList()//获取分类--子分类二位数组
        if (this.$route.query.id) {
            this.id = this.$route.query.id
            this.getProDetail(this.$route.query.id)
        }
    },
    mounted() {
        window.addEventListener("scroll", this.handleScroll);
    },
    methods: {
        //获取详情
        getProDetail(id) {
            getProdInfo(id).then(response => {
                this.form = response.data;
            });
        },
        //获取供应商下拉列表
        getSupplierList() {
            supplierSelectList().then(res => {
                if (res.code == 200) {
                    this.suppLiserList = res.data
                }
            })
        },
        //获取品牌下拉列表
        getBrandList() {
            brandSelectList().then(res => {
                if (res.code == 200) {
                    this.brandList = res.data
                }
            })
        },
        //获取分类--子分类二位数组
        getClassList() {
            getClassList().then(res => {
                if (res.code == 200) {
                    this.classList = res.data
                }
            })
        },
        //选择分类-筛选子分类
        changeClass(val) {
            this.subClassList = []
            this.$set(this.form, 'sedCate', '')
            this.classList.forEach(item => {
                if (item.id == val) {
                    this.subClassList = item.list
                }
            })
        },
        //输入名称转拼音
        inputName(val) {
            this.form.pinyin = pinyin.getCamelChars(val)
        },
        //图片上传成功
        handleAvatarSuccess(res, file) {
            this.form.url = res.url
        },
        // 返回上一页
        back() {
            this.$router.push({ path: '/goods/prodInfo' })
        },
        //表单提交
        onSubmit() {
            if (this.id) {
                updateProdInfo(this.form).then(response => {
                    this.$modal.msgSuccess("编辑成功");
                });
                this.back()
            } else {
                this.form.sellByDate = this.form.sellByDate + this.form.dateUnit
                addProdInfo(this.form).then(response => {
                    this.$modal.msgSuccess("添加成功");
                });
                this.back()
            }
        },
        //页面滚动
        jumpTo(ele) {
            this.tab = ele
            let targetbox = document.getElementById(ele);
            this.target = targetbox.offsetTop;
            document.documentElement.scrollTop = this.target
        },
        //页面滚动
        handleScroll() {
            let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
            var basicTop = document.querySelector("#basic").offsetTop
            var settingTop = document.querySelector("#setting").offsetTop
            var moreTop = document.querySelector("#more").offsetTop
            // console.log(scrollTop,moreTop)
            if (scrollTop > moreTop) {
                this.tab = 'more'
            } else if (scrollTop > settingTop) {
                this.tab = 'setting'
            } else if (scrollTop > basicTop) {
                this.tab = 'basic'
            }
        },
        // 失去焦点事件
        onEditorBlur(quill) {
            console.log('editor blur!', quill)
        },
        // 获得焦点事件
        onEditorFocus(quill) {
            console.log('editor focus!', quill)
        },
        // 准备富文本编辑器
        onEditorReady(quill) {
            console.log('editor ready!', quill)
        },
        // 内容改变事件
        onEditorChange({ quill, html, text }) {
            console.log('editor change!', quill, html, text)
            this.content = html
        },
        //新增分类
        addSort() {
            this.form.topCate ? this.srotDialogTitle = '新增子分类' : this.srotDialogTitle = '新增分类'
            this.sortDialogVisible = true
        },
        //新增品牌
        addBrand() {
            this.srotDialogTitle = '新增品牌'
            this.sortDialogVisible = true
        },
        //新增供应商
        addSupplier() {
            this.srotDialogTitle = '新增供应商'
            this.sortDialogVisible = true
        },
        //提交分类-品牌-供应商新增弹窗
        submitForm() {

        },
    }
};
</script>
<style scoped lang="scss">
.box {
    position: relative;

    .left {
        width: 80%;
    }

    .right {
        ul {
            list-style: none;
            height: 300px;
            border-left: 2px solid #E4E7ED;
            position: fixed;
            right: 40px;
            top: 200px;
            padding-left: 0;
        }

        li {
            padding: 0 20px;
            height: 40px;
            box-sizing: border-box;
            line-height: 40px;
            list-style: none;
            font-size: 14px;
            font-weight: 500;
            color: #303133;
            position: relative;
            left: -2px;
            box-sizing: border-box;
            border-left: 2px solid transparent;
        }

        li:hover {
            cursor: pointer;
            color: #409eff;
        }

        li.active {
            color: #409eff;
            border-color: #409eff;
        }
    }

    .prod-title {
        .basic {
            font-size: 18px;
            color: #333336;
            font-weight: 700;

            span {
                color: #409eff;
                font-size: 12px;
                font-weight: normal;
                margin: 0 20px 0 5px;
            }
        }
    }

    .prod-content {
        margin-top: 20px;

        div {
            margin-bottom: 15px;

            .mr {
                margin-right: 10px;
            }

            p {
                color: #000000;
                font-size: 14px;

                span {
                    color: #b3b3b3;
                    font-size: 12px;
                }
            }

            .el-input {
                width: 240px;
            }

            .select {
                margin-left: 10px;
                width: 80px;
            }

            .el-textarea {
                width: 500px;
            }

            .el-button.btn {
                margin-left: 10px;
            }

            .tip {
                display: inline-block;
                font-size: 12px;
                color: #333336;
                margin-left: 5px;

                span {
                    color: #409eff;
                }

                span:hover {
                    cursor: pointer;
                }
            }

        }

        .flex {
            p {
                margin-top: 0 !important;
            }
        }

        .suggest {
            span {
                color: rgb(245, 108, 108);
                font-size: 12px;
            }
        }
    }
}
</style>
